﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace ipi.MCC.Controls
{
    public abstract class MCCViewGrid : UserControl
    {
        protected ObjectDataSource odsMCCContent;
        protected SPGridView gvMCCContent;
        protected MenuTemplate mtMCCContent;
        protected HiddenField hiddenSource;
        protected HiddenField hiddenSelectedNode;
        protected CustomValidator validGlobal;

        protected MenuItemTemplate mitListPropertiesMenu;
        protected MenuItemTemplate mitListVersionsMenu;

        protected ImageField ifMain;
        protected SPMenuField smfName;
        protected BoundField bfTitle;

        public void SetSelectedNode(int iSelectedNode)
        {
            if (hiddenSelectedNode != null)
            {
                hiddenSelectedNode.Value = Convert.ToString(iSelectedNode);
            }
        }

        public int PagingCount { get; set; }
        public bool EnablePaging { get; set; }

        protected abstract string GetMethod { get; }
        protected virtual bool GetPropertiesMenuVisibiliy
        {
            get { return true; }
        }

        protected virtual string mtMCCContentID
        {
            get { return "mtMCCContentID";  }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.Controls.Clear();
            validGlobal = new CustomValidator();
            validGlobal.Display = ValidatorDisplay.Dynamic;
            this.Controls.Add(validGlobal);
            
            try
            {
                hiddenSource = new HiddenField();
                hiddenSource.ID = "hiddenSource";
                hiddenSource.Value = SPEncode.UrlEncode(Request.Url.AbsoluteUri);
                hiddenSelectedNode = new HiddenField();
                hiddenSelectedNode.ID = "hiddenSelectedNode";
                //hiddenSelectedNode.Value = Convert.ToString(this.SelectedNode);
                
                mtMCCContent = new MenuTemplate();
                mtMCCContent.ID = mtMCCContentID;
                mtMCCContent.EnableSorting = true;
                
                mitListPropertiesMenu = new MenuItemTemplate();
                mitListPropertiesMenu.Text = Resources.Resources.MCCDocumentView_Menu_ViewProperties;
                mitListPropertiesMenu.ImageUrl="~/_layouts/images/EditItem.gif";
                mitListPropertiesMenu.ClientOnClickNavigateUrl = "%VIEWURL%";

                mitListVersionsMenu = new MenuItemTemplate();
                mitListVersionsMenu.Text = Resources.Resources.MCCDocumentView_Menu_ViewVersion;                
                mitListVersionsMenu.ImageUrl="~/_layouts/images/versions.gif";
                mitListVersionsMenu.ClientOnClickNavigateUrl="%VIEWVERSIONSURL%"; 
                mitListVersionsMenu.HiddenScript = ""; 
                mitListVersionsMenu.PermissionsString="ViewVersions";
                mitListVersionsMenu.PermissionMode = PermissionMode.All;
                mitListVersionsMenu.PermissionContext = PermissionContext.CurrentSite;

                if (GetPropertiesMenuVisibiliy)
                {
                    mtMCCContent.Controls.Add(mitListPropertiesMenu);
                }
                mtMCCContent.Controls.Add(mitListVersionsMenu);

                odsMCCContent = new ObjectDataSource();
                odsMCCContent.Selected += new ObjectDataSourceStatusEventHandler(odsMCCContent_Selected);
                odsMCCContent.ID = "odsMCCContent";
                odsMCCContent.TypeName = "ipi.MCC.Global.MCCData, MCC.Global, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bc7fbd2db981757a";
                odsMCCContent.SelectMethod = this.GetMethod;
                odsMCCContent.SelectParameters.Add(new ControlParameter("CurrentNodeID", TypeCode.Int32, hiddenSelectedNode.ID, "Value"));
                odsMCCContent.SelectParameters.Add(new ControlParameter("Source", TypeCode.String, hiddenSource.ID, "Value"));

                gvMCCContent = new SPGridView();
                gvMCCContent.PageIndexChanging += new GridViewPageEventHandler(gvDocumentView_PageIndexChanging);

                gvMCCContent.Columns.Clear();

                ifMain = new ImageField();
                ifMain.HeaderText = Resources.Resources.MCCDocumentView_Image;
                ifMain.DataImageUrlField = "DocIconPath";
                ifMain.SortExpression = "DocIconPath";
                ifMain.ItemStyle.Width = Unit.Pixel(30);
                ifMain.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                gvMCCContent.Columns.Add(ifMain);

                smfName = new SPMenuField();
                smfName.HeaderText = Resources.Resources.MCCDocumentView_FileName;
                smfName.TextFields = "LinkFilename";
                smfName.MenuTemplateId = mtMCCContent.ID;
                smfName.NavigateUrlFields = "FileURL";
                smfName.NavigateUrlFormat = "{0}";
                smfName.TokenNameAndValueFields = "EDIT=ID,URL=FileURL,VIEWURL=ViewPropertiesURL,VIEWVERSIONSURL=VersionsURL";
                smfName.SortExpression = "LinkFilename";
                gvMCCContent.Columns.Add(smfName);

                bfTitle = new BoundField();
                bfTitle.HeaderText = Resources.Resources.MCCDocumentView_Title;
                bfTitle.DataField = "Title";
                bfTitle.SortExpression = "Title";
                gvMCCContent.Columns.Add(bfTitle);
                //                }

                gvMCCContent.AutoGenerateColumns = false;
                gvMCCContent.DataSourceID = odsMCCContent.ID; 
                gvMCCContent.AllowSorting= true;
                gvMCCContent.AllowFiltering=false;
                gvMCCContent.AllowPaging=true;
                gvMCCContent.Width = Unit.Percentage(100);
                gvMCCContent.FilterDataFields = "DocIcon, LinkFilename, Title";
                gvMCCContent.FilteredDataSourcePropertyName = "FilterExpression";
                gvMCCContent.FilteredDataSourcePropertyFormat = "{1} LIKE '%{0}%'";
                gvMCCContent.EmptyDataText = Resources.Resources.MCCDocumentView_EmptyResult;
                
                gvMCCContent.PageSize = this.PagingCount;

                if (this.EnablePaging == true)
                {
                    gvMCCContent.PagerSettings.Visible = true;
                }
                else
                {
                    gvMCCContent.PagerSettings.Visible = false;
                }

                this.Controls.Add(hiddenSelectedNode);
                this.Controls.Add(hiddenSource);
                this.Controls.Add(odsMCCContent);
                this.Controls.Add(gvMCCContent);
                this.Controls.Add(mtMCCContent);

                gvMCCContent.PagerTemplate = null;
            }
            catch (Exception exc)
            {
                validGlobal.ErrorMessage = exc.Message;
                validGlobal.IsValid = false;
            }
        }

        protected void odsMCCContent_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.Exception != null)
            {
                validGlobal.ErrorMessage = e.Exception.Message;
                validGlobal.IsValid = false;

                e.ExceptionHandled = true;
            }
        }

        protected void gvDocumentView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMCCContent.PageIndex = e.NewPageIndex;
            //gvMCCContent.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            gvMCCContent.DataBind();
        }
    }
}
